@extends('admin::layouts.master')

@section('title', __('MultiSeller::seller_group.title'))

@section('content')
  @if ($errors->has('error'))
    <x-admin-alert type="danger" msg="{{ $errors->first('error') }}" class="mt-4" />
  @endif
  @if ($errors->any())
    <div class="alert alert-danger">
      @foreach ($errors->all() as $error)
        <div>{{ $error }}</div>
      @endforeach
    </div>
  @endif

  <div id="shipping-app" class="card">
    <div class="card-header">{{ __('MultiSeller::seller_group.title') }}</div>
    <div class="card-body">
      <form class="needs-validation" novalidate action="{{ $seller_group->id ? admin_route('seller_groups.update', $seller_group) : admin_route('seller_groups.store') }}"
        method="POST">
        @csrf
        @method($seller_group->id ? 'PUT' : 'POST')

        <x-admin::form.row :title="__('common.name')">
          @foreach (locales() as $language)
          <div class="input-group w-max-600">
            <span class="input-group-text wp-100">{{ $language['name'] }}</span>
            <input type="text" name="name[{{ $language['code'] }}]" value="{{ old('name', $seller_group->descriptions->keyBy('locale')[$language['code']]->name ?? '') }}" class="form-control short" placeholder="{{$language['name']}}" required>
          </div>
          @endforeach
        </x-admin::form.row>

        <x-admin::form.row :title="__('admin/region.describe')">
          @foreach (locales() as $language)
          <div class="input-group w-max-600">
            <span class="input-group-text wp-100">{{ $language['name'] }}</span>
            <textarea rows="2" type="text" name="description[{{ $language['code'] }}]" class="form-control wp-400" placeholder="{{ $language['name'] }}">{{ old('meta_title', $seller_group->descriptions->keyBy('locale')[$language['code']]->description ?? '') }}</textarea>
          </div>
          @endforeach
        </x-admin::form.row>

        <x-seller-form-input name="fee_sale_flat" :error="$errors->first('fee_sale_flat')" type="number" :title="__('MultiSeller::seller_group.fee_sale_flat')" :value="old('fee_sale_flat', $seller_group->fee_sale_flat ?? '')" />

        <x-seller::form.row :title="__('MultiSeller::seller_group.fee_sale_percent')">
          <div class="input-group wp-400">
            <span class="input-group-text">%</span>
            <input type="number" class="form-control" value="{{ old('fee_sale_percent', $seller_group->fee_sale_percent ?? '') }}" name="fee_sale_percent" placeholder="{{ __('MultiSeller::seller_group.fee_sale_percent') }}" >
            <div class="invalid-feedback">{{ __('common.error_required', ['name' => __('MultiSeller::seller_group.fee_sale_percent')]) }}</div>
          </div>
        </x-seller::form.row>

        <x-seller::form.row>
          <button type="submit" class="btn btn-primary btn-lg mt-3">{{ __('common.save') }}</button>
        </x-seller::form.row>
      </form>

    </div>
  </div>
@endsection

@push('footer')
  <script>
    $('input[name="fee_sale_percent"]').on('input', function () {
      var val = $(this).val();
      if (val > 100) {
        $(this).val(100);
      }
    });
  </script>
@endpush